package cn.xxm.dongbao.user.secure.config;

import cn.xxm.dongbao.user.secure.dos.DosPrecautionFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class FilterConfig {
    private static final String DOSURL_UMSMEMBER = "/user/umsmember/list";
    private static final String HITS = "10";
    private static final String PERIOD = "10";
    private static final String ACCESS_DENIED_HANDLER = "cn.xxm.dongbao.user.secure.handler.DosErrorHandler";

    /**
     * DosPrecautionFilter
     * @return DosPrecautionFilter
     */
    @Bean
    public DosPrecautionFilter dosPrecautionFilter() {
        return new DosPrecautionFilter();
    }

    /**
     * 注册 dosPrecautionFilter
     * @return FilterRegistrationBean
     */
    @Bean
    public FilterRegistrationBean dosErrorHandlerFilterRegist() {
        FilterRegistrationBean frBean = new FilterRegistrationBean();
        frBean.setFilter(dosPrecautionFilter());
        frBean.setOrder(4);
        frBean.addUrlPatterns(DOSURL_UMSMEMBER);
        Map<String, String> map = new HashMap<>();
        map.put("hits", HITS);
        map.put("period", PERIOD);
        map.put("access_denied_handler", ACCESS_DENIED_HANDLER);
        frBean.setInitParameters(map);
        return frBean;
    }
}